package com.inossem.warehousestorage_inossem.activity.storage;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.inossem.utils.base.Utils;
import com.inossem.utils.listener.NoDoubleClickListener;
import com.inossem.warehousestorage_inossem.R;
import com.inossem.warehousestorage_inossem.activity.base.BaseActivity;
import com.inossem.warehousestorage_inossem.adapter.OverviewAdapter;
import com.inossem.warehousestorage_inossem.bean.storage.StorageManageMatrBean;
import com.inossem.warehousestorage_inossem.bean.storage.StorageManageMatrCommitBean;
import com.inossem.warehousestorage_inossem.bean.storage.StorageManageRespBean;
import com.inossem.warehousestorage_inossem.constant.RequestConstant;
import com.inossem.warehousestorage_inossem.dialog.DialogUtils;
import com.inossem.warehousestorage_inossem.listener.GsonBeanListener;
import com.inossem.warehousestorage_inossem.request.storage.StorageManageMatrRequest;
import com.inossem.warehousestorage_inossem.utils.ActivityUtils;
import com.inossem.warehousestorage_inossem.utils.ReceiveUtils;
import com.inossem.warehousestorage_inossem.utils.SPUtils;
import com.inossem.warehousestorage_inossem.utils.StorageManageUtils;
import com.zhy.android.percent.support.PercentRelativeLayout;

import java.util.List;

/**
 * Created by Administrator on 2017/7/16 0016.
 */

public class StorageManageMatrCommitActivity extends BaseActivity {
    private TextView title,doUp,operator;
    private TextView confirm;
    private StorageManageMatrBean bean;
    private StorageManageMatrCommitBean commitBean;
    private PercentRelativeLayout secondBlock;
    private List<OverviewAdapter.OverviewBean> overViewlist;


    @Override
    protected void onCreateImpl(Bundle savedInstanceState) {
        setContentView(R.layout.activity_storage_manage_matr_commit);
        initView(this);
        initData(this);
    }


    private void initView(Activity activity) {
        title = (TextView) activity.findViewById(R.id.headerTitle);
        doUp = (TextView) activity.findViewById(R.id.doUp);
        confirm = (TextView)activity.findViewById(R.id.confirm);
        operator = (TextView)activity.findViewById(R.id.operator);
        secondBlock = (PercentRelativeLayout)activity.findViewById(R.id.secondBlock);
        confirm.setOnClickListener(new NoDoubleClickListener() {
            @Override
            public void onNoDoubleClick(View v) {
                StorageManageMatrRequest.commit(activity,commitBean.getData(),new GsonBeanListener<StorageManageRespBean>() {
                    @Override
                    public void onSuccess(StorageManageRespBean bean) {
                        if(bean.getData().getCode().equals(RequestConstant.BEAN_CODE_SUCCESS) ){
                            SPUtils.removeInformation(activity,SPUtils.KEY_STORAGE_MGR_MATR_INFORMATION);
                            Intent intent = new Intent(activity, StorageManageResultActivity.class);
                            startActivity(intent);
                        }else{
                            Utils.middleToast(activity, "提交失败:"+bean.getData().getMsg());
                        }

                    }
                });
            }
        });
    }

    private void initData(Activity activity) {
        title.setText(activity.getResources().getString(R.string.activity_up_frame_commit_title));
        ActivityUtils.initBackAndFunction(activity, R.string.activity_up_frame_commit_back, new NoDoubleClickListener() {
            @Override
            public void onNoDoubleClick(View v) {
                finish();
            }
        });
        secondBlock.setOnClickListener(new NoDoubleClickListener() {
            @Override
            public void onNoDoubleClick(View v) {
                DialogUtils.getOverviewDialog(activity, overViewlist).show();
            }
        });
        getData(activity);
    }


    private void getData(Context context) {
        bean =  SPUtils.getInformation(context, StorageManageMatrBean.class,SPUtils.KEY_STORAGE_MGR_MATR_INFORMATION);
        commitBean = StorageManageUtils.getCommitBean(bean);
        overViewlist = StorageManageUtils.getOverViewList(bean);
        doUp.setText(commitBean.getData().getMaterielList().size()+"");
        operator.setText(SPUtils.getName(this));
    }


    @Override
    public void onBackPressed() {
        finish();
    }
}
